package dp.ua.epic.pages;

import dp.ua.epic.webapp.EpicSession;
import dp.ua.epic.webapp.EpicApplication;
import java.io.Serializable;
import java.util.Locale;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;

/**
 * Class that represent base behavior for all pages
 * 
 * @author Chris
 * @date 25.01.2011 19:18:41
 */
public abstract class BasePage extends WebPage {

    public BasePage() {
        add(new Label("pageTitle", new StringResourceModel("basePage.title", new Model())));

        add(new BookmarkablePageLink("startPageLink", Start.class));
        add(new BookmarkablePageLink("choosePageLink", Choose.class));
        add(new BookmarkablePageLink("chosenPageLink", Chosen.class));

        add(new ListView("languages", new Model() {

            @Override
            public Serializable getObject() {
                return (Serializable) EpicApplication.aviableLocales;
            }
        }) {

            @Override
            protected void populateItem(ListItem item) {
                final Locale locale = (Locale) item.getModelObject();
                Link link = new Link("setLangLink") {

                    @Override
                    public void onClick() {
                        getSession().setLocale(locale);
                        setRedirect(true);
                        setResponsePage(getWebPage().getPage().getClass());
                    }
                };
                link.add(new Label("languageName", new Model(capitalize(locale.getDisplayLanguage()) + "(" + locale.getDisplayCountry() + ")")));
                item.add(link);
            }
        });
    }

    private String capitalize(String input) {
        return input.substring(0, 1).toUpperCase() + input.substring(1);
    }

    public EpicSession getEpicSession() {
        return (EpicSession) getSession();
    }
}
